package com.threeonemeeting.activity;

import java.util.Map;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.snda.lib.http.ICallBack;
import com.snda.lib.ui.MessageBox;
import com.snda.lib.util.MiscHelper;
import com.threeonemeeting.DataCenter;
import com.threeonemeeting.R;
import com.threeonemeeting.tab.BaseTabActivity;
import com.threeonemeeting.tab.MultiActivityTab;
import com.threeonemeeting.util.MiscUtil;
import com.weibo.sina.OAuthConstant;
import com.weibo.sina.Weibo;
import com.weibo.sina.WeiboException;
import com.weibo.sina.http.RequestToken;
import com.weibo.tencent.QWeibo;

public class WeiboAccountActivity extends BaseTabActivity implements ICallBack {

	public static final int DIALOG_PROGRESS = 1;

	public static final int WEIBO_AUTH = 1;
	public static final int WEIBO_SEND = 2;
	public static final int WEIBO_GETTOKEN = 3;

	protected MessageBox mMessageBox;
	protected Dialog loadingDialog;

	public static final String URL_ACTIVITY_WEIBO_CALLBACK = "weiboandroidsdk://weibo_host";
	public static final String URL_ACTIVITY_QWEIBO_CALLBACK = "weiboandroidsdk://qweibo_host";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_weibo_account);

		System.setProperty("weibo4j.oauth.consumerKey",
				DataCenter.SINA_CONSUMER_KEY);
		System.setProperty("weibo4j.oauth.consumerSecret",
				DataCenter.SINA_CONSUMER_SECRET);

		refresh();
	}

	/**
	 * @desc 对界面进行重新加载
	 */
	@Override
	public void refresh() {
		init("微博帐号绑定");

		mMessageBox = new MessageBox(
				(MultiActivityTab) WeiboAccountActivity.this.getParent());

		// 检查是否已经绑定
		String strSinaWeibo = MiscUtil.getPrefer("SinaWeiboToken");
		if (strSinaWeibo != null && strSinaWeibo.equals("") == false) {
			TextView txView = (TextView) findViewById(R.id.tx_sina_weibo_status);
			txView.setText("已绑定");
		}

		String strQQWeibo = MiscUtil.getPrefer("QQWeiboToken");
		if (strQQWeibo != null && strQQWeibo.equals("") == false) {
			TextView txView = (TextView) findViewById(R.id.tx_qq_weibo_status);
			txView.setText("已绑定");
		}

	}

	private Handler uiHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				loadingDialog.dismiss();
				break;
			case 1:
				loadingDialog.dismiss();
				MessageBox.showToast(getApplicationContext(), "连接腾讯微博失败");
				break;

			case 2:
				loadingDialog.dismiss();
				MessageBox.showToast(getApplicationContext(), "连接新浪微博失败");
				break;
			}
		}
	};

	public void OnClickSina(View arg0) {

		loadingDialog = MessageBox.showProcessDlg(
				(MultiActivityTab) WeiboAccountActivity.this.getParent(),
				"正在连接新浪微博，请稍候...");
		loadingDialog.show();

		Runnable runable = new Runnable() {
			public void run() {
				Weibo weibo = OAuthConstant.getInstance().createWeiboInstance();

				RequestToken requestToken;

				try {
					requestToken = weibo
							.getOAuthRequestToken(URL_ACTIVITY_WEIBO_CALLBACK);
					OAuthConstant.getInstance().setRequestToken(requestToken);
					uiHandler.sendEmptyMessage(0);

					String strUrl = requestToken.getAuthenticationURL();;
					Intent intent = new Intent(WeiboAccountActivity.this,
							WebViewActivity.class);
					Bundle b = new Bundle();
					b.putString("url", strUrl);
					intent.putExtras(b);
					startActivity(intent);
					uiHandler.sendEmptyMessage(0);

				} catch (WeiboException e) {
					e.printStackTrace();
					uiHandler.sendEmptyMessage(2);
				} catch (Exception e) {
					uiHandler.sendEmptyMessage(2);
				}
			}
		};
		new Thread(runable).start();
	}

	public void OnClickTencent(View arg0) {
		loadingDialog = MessageBox.showProcessDlg(
				(MultiActivityTab) WeiboAccountActivity.this.getParent(),
				"正在连接腾讯微博，请稍候...");
		loadingDialog.show();

		Runnable runable = new Runnable() {

			public void run() {

				QWeibo.getInstance().setConsumeKey(DataCenter.QQ_CONSUMER_KEY);
				QWeibo.getInstance().setConsumeSecret(
						DataCenter.QQ_CONSUMER_SECRET);
				QWeibo.getInstance().setCallBackUrl(
						URL_ACTIVITY_QWEIBO_CALLBACK);

				String strTokenKey = QWeibo.getInstance().requestRequestToken();
				if (MiscHelper.isEmpty(strTokenKey) == true) {
					uiHandler.sendEmptyMessage(1);
					return;
				}

				String strUrl = "http://open.t.qq.com/cgi-bin/authorize?oauth_token="
						+ strTokenKey;
				Intent intent = new Intent(WeiboAccountActivity.this,
						WebViewActivity.class);
				Bundle b = new Bundle();
				b.putString("url", strUrl);
				intent.putExtras(b);
				startActivity(intent);
				uiHandler.sendEmptyMessage(0);
			}
		};
		new Thread(runable).start();
	}

	public void doCallBack(Map<String, Object> map) {

	}

}
